/*
 * 项目组:
 *      =       =       =      =
 *       =     = =     =       =
 *        =   =   =   =        =
 *         = =     = =         =
 *          =       =          = = = = 
 */
$(function () {

    $(".choose").prop("checked", false);
    $.ajax({
        url: "/order/order/findshoppingcart",
        dataType: "json",
        type: "post",
        success: function (result) {
            if (result.status !== 2001) {
                alert(result.msg);
            } else {
                $("#purchasenums").text("购物车(" + result.data.length + ")")
                for (const resultElement of result.data) {
                    let page = '<ul class="am-g list">' +
                        '<span hidden>' + resultElement.bookId + '</span>' +
                        '<li class="am-u-sm-2">' +
                        ' <span><input type="checkbox" class="choose"></span>' +
                        '<span class="img"><img src="' + resultElement.imgurl + '"></span>' +
                        ' </li>' +
                        '<li class="am-u-sm-2">' + resultElement.bookname + '</li>' +
                        ' <li class="am-u-sm-2 price">￥' + resultElement.price + '</li>' +
                        '<li class="am-u-sm-2 price">￥' + resultElement.price + '</li>' +
                        '<li class="am-u-sm-2 num">' +
                        ' <span>-</span>' +
                        '<span>' + resultElement.purchasenums + '</span>' +
                        '<span>+</span>' +
                        '</li>' +
                        '<li class="am-u-sm-2 price">￥' + resultElement.totalmoney + '</li>' +
                        ' <li class="am-u-sm-1 am-u-end">删除</li>' +
                        '</ul>';
                    let _page = $(page);
                    _page.insertAfter($(".total"));
                }

            }
        }
    });


    $("#stepTwo .choose").prop("disabled", true);
    $("#stepThree .choose").prop("disabled", true);
    $("body").on("click", ".choose", function () {
        let selectedindex = [];
        let $choose = $("#stepOne .choose");

        let flag = true;
        for (let i = 0; i < $choose.length; i++) {
            $("#stepTwo .choose").eq(i).prop("checked", $($choose[i]).prop("checked"));
            $("#stepThree .choose").eq(i).prop("checked", $($choose[i]).prop("checked"));
            if ($($choose[i]).prop("checked") === false) {
                flag = false;
            } else {
                let total = $($choose[i]).parent().parent().nextAll(".price:last").text().replace("￥", "");
                selectedindex.push(total);
            }
        }
        if (flag) {
            $(".all-check").prop("checked", true);
        } else {
            $(".all-check").prop("checked", false);
        }
        if (selectedindex.length === 0) {
            $(".payment").text("￥0");
            $("#stepOne .accout").prop("disabled", true);
        } else {
            $("#stepOne .accout").prop("disabled", false);
            $.ajax({
                url: "/order/order/countmoney",
                dataType: "json",
                type: "post",
                data: {
                    "selectedIndexArrayStr": JSON.stringify(selectedindex)
                },
                success: function (result) {
                    $(".payment").text("￥" + result.data);
                }
            })
        }


    });

    $(".all-check").click(function () {
            $(".all-check,.choose").prop("checked", $(this).prop("checked"));
        }
    );

    $("#submit-order").click(function () {
        let selectedindex = [];
        let $choose = $("#stepTwo .choose");

        for (let i = 0; i < $choose.length; i++) {
            if ($($choose[i]).prop("checked") === true) {
                let total = $($choose[i]).parent().parent().prev(":hidden").text();
                selectedindex.push(total);
            }
        }
        console.log(selectedindex);
        $.ajax({
            url: "/order/order/generateOrder",
            dataType: "json",
            type: "post",
            data: {
                "selectedIndexArrayStr": JSON.stringify(selectedindex)
            },
            success: function (result) {
                $(".order-no").text(result.data.no);
                $(".order-user").text("mofeibai");
                $(".user-phone").text(result.data.user.phone);
            }
        })

    });


    changeStep();
    bindStepThreeButton();


    // 事件应为动态绑定
    bindStepOneButton();
    bindStepTwoButton();
    // 事件应为动态绑定

    let websocket = new WebSocket("ws://localhost:9007/socketServer/1");
    websocket.onmessage = function (event) {
        console.log(event);
        if (event.data === "1") {
            location.href = "http://localhost:9007/order/showorderdetails";
        }
    }

});

// 订单提交步骤
function changeStep(step) {
    $("#executionSteps li").removeClass("active");
    $("#stepOne").css("display", "none");
    $("#stepTwo").css("display", "none");
    $("#stepThree").css("display", "none");
    if (!step) {
        $("#stepOne").css("display", "block");
        $("#executionSteps li[name=stepOne]").addClass("active");
    }
    if (step) {
        $("#" + step).css("display", "block");
        $("#executionSteps li[name=" + step + "]").addClass("active");

    }

}

function bindStepOneButton() {
    $("#stepOne .acount").on("click", function () {
        changeStep("stepTwo");
    });
}

function bindStepTwoButton() {
    $("#stepTwo .acount").on("click", function () {
        changeStep("stepThree");
    });
}

function bindStepThreeButton() {
    // 打开二维码弹窗
    $("#stepThree .acount").on("click", function () {
        $("#scavengingPayment").modal('open')
    });
}   